package me.zhengjie.modules.system.rest;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Clazzandstudent;
import me.zhengjie.modules.system.domain.Student;
import me.zhengjie.modules.system.domain.Teach;
import me.zhengjie.modules.system.domain.vo.ClazzanddepartmentQueryCriteria;
import me.zhengjie.modules.system.service.ClazzService;
import me.zhengjie.modules.system.service.ClazzandstudentService;
import me.zhengjie.modules.system.service.ClazzandteskService;
import me.zhengjie.modules.system.service.StudentService;
import me.zhengjie.utils.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "班级学生管理")
@RequestMapping("/api/clazzandstudent")
public class ClazzandstudentController {

    private final ClazzandstudentService clazzandstudentService;

    private final StudentService studentService;

    //获取班级学生 传入班级id
    @Log("获取班级学生")
    @ApiOperation("班级学生")
    @GetMapping("/getClazzStudent")
    public ResponseEntity<List<Student>> getClazzStudent(@Param("clazzId")  Integer clazzId) {

        List<Student> studentList = clazzandstudentService.getClazzStudent(clazzId);
        return new ResponseEntity<>(studentList, HttpStatus.OK);
    }


    //获取未进入班级学生 进入该课程 未进入班级的学生 传入课程id
    @GetMapping("/getNotClazzStudent")
    @Log("获取未进入班级学生")
    @ApiOperation("未进入班级学生")
    public ResponseEntity<List<Student>> getNotClazzStudent(@Param("courseId") Integer courseId) {
        List<Student> studentList = clazzandstudentService.getNotClazzStudent(courseId);
        return new ResponseEntity<>(studentList, HttpStatus.OK);
    }
    //添加班级学生
    @Log("添加班级学生")
    @ApiOperation("添加班级学生")
    @PostMapping("/createClazzStudent")
    public ResponseEntity<Object> createClazzStudent(@RequestBody Student student) {
        Integer status = studentService.create(student);
        Map<String,Object> map = new HashMap<>();
        map.put("status",4000);
        if(status == -1){
            map.put("msg","邮箱重复");
            return new ResponseEntity<>(map, HttpStatus.OK);
        }
        if(status == -3){
            map.put("msg","用户名已存在");
            return new ResponseEntity<>(map, HttpStatus.OK);
        }
        if(status == -4){
            map.put("msg","手机号重复");
            return new ResponseEntity<>(map, HttpStatus.OK);
        }
        if(status == -5){
            map.put("msg","学号重复");
            return new ResponseEntity<>(map, HttpStatus.OK);
        }



        Clazzandstudent clazzandstudent = new Clazzandstudent();
        clazzandstudent.setStudentId(student.getId());
        clazzandstudent.setClazzId(Long.valueOf(student.getClazzId()));
        clazzandstudentService.save(clazzandstudent);
        map.put("status",200);
        map.put("msg","新增成功");
        return new ResponseEntity<>(map, HttpStatus.OK);
    }


    @Log("获取班级学生")
    @ApiOperation("班级学生")
    @GetMapping("/getNotClazzStudentList")
    //获取不在班级中的学生
    public List<Student> getNotClazzStudentList(ClazzanddepartmentQueryCriteria criteria){


        return clazzandstudentService.getNotClazzStudentList(criteria);
    }



    //绑定班级学生
    @PostMapping
    public void bindClazzStudent(@RequestBody List<Clazzandstudent> clazzandstudents) {
        //查询该班级所有学生

        //如果传入学生不存在则添加

        //如果在查询之中不存在删除
        for(Clazzandstudent clazzandstudent :clazzandstudents){
            clazzandstudentService.save(clazzandstudent);
        }
    }

    @DeleteMapping
    public ResponseEntity<Object> deleteClazzandtesk(@RequestBody List<Clazzandstudent> clazzandstudents) {

        if(!clazzandstudents.isEmpty()){
            List<Long> deleteIds = new ArrayList<>();
            for(Clazzandstudent clazzandstudent:clazzandstudents){
                deleteIds.add(clazzandstudent.getStudentId());
            }
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("clazzId",clazzandstudents.get(0).getClazzId());
            queryWrapper.in("studentId",deleteIds);
            clazzandstudentService.remove(queryWrapper);
        }

        return new ResponseEntity<>(HttpStatus.OK);
    }
}
